/**
 * @author of2198
 * @date 2017/5/4
 * @desc 加载js 和 css 的工具类
 */

class LoadResourceUtil {

    static loadJS (filename) {
        //单页面可能会重复append  先删除
        let scripts = document.scripts;
        for (let i=0; i<scripts.length; i++) {
            if (scripts[i].getAttribute('src') == filename) {
                scripts[i].remove();
            }
        }

        let head  = document.getElementsByTagName('head')[0];
        let script  = document.createElement('script');
        script.type = 'text/javascript';
        script.charset='utf-8';
        script.async =true;
        script.src = filename;
        head.appendChild(script);
    }

    static loadCSS (filename) {
        //单页面可能会重复append  先删除
        let links = document.getElementsByTagName('link');
        for (let i=0; i<links.length; i++) {
            if (links[i].getAttribute('href') == filename) {
                links[i].remove();
            }
        }

        let head  = document.getElementsByTagName('head')[0];
        let link = document.createElement('link');
        link.rel = 'stylesheet';
        link.type = 'text/css';
        link.href= filename;
        head.appendChild(link);
    }
}

export default LoadResourceUtil;